package com.taiyi.renaissancemall.ui.classification;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.taiyi.renaissancemall.R;
import com.taiyi.renaissancemall.Utils.BannerViewHolder;
import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.base.BaseFragment;
import com.taiyi.renaissancemall.bean.FirstClassBean;
import com.taiyi.renaissancemall.bean.NextClassBean;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

//分类页面
public class ClassFragment extends BaseFragment<ClassView, ClassPresent> implements ClassView {
    @BindView(R.id.mall)
    TextView mall;
    @BindView(R.id.lv)
    ListView lv;
    @BindView(R.id.carouselView)
    ImageView carouselView;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private List<FirstClassBean.ClassificationlistBean> left_data = new ArrayList<>();
    private List<FirstClassBean.ClassificationlistBean> right_data = new ArrayList<>();
    private List<String> img_urls = new ArrayList<>();

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(mall).statusBarDarkFont(true, 0f).init();
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            mImmersionBar.titleBar(mall).statusBarDarkFont(true, 0f).init();
        }
    }

    @Override
    public ClassPresent initPresenter() {
        return new ClassPresent();
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        showProgress();
        //一级列表
        presenter.getIndexClass();
    }

    @Override
    protected void initData() {

    }

    @Override
    public void initEvent() {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.class_fragment_layout;
    }

    @Override
    public void indexClassSuccess(FirstClassBean firstClassBean) {
hideProgress();
        firstClassBean.classificationlist.get(0).isSelected = true;
        FirstClassAdapter firstClassAdapter = new FirstClassAdapter(firstClassBean, getActivity());
        lv.setAdapter(firstClassAdapter);

        if (img_urls.size() > 0) {
            img_urls.clear();
        }
        img_urls.add(firstClassBean.classificationlist.get(0).imgurl);
        showBanner(img_urls);
        firstClassBean.classificationlist.get(0).isSelected = true;
        //二三级列表
        presenter.getIndexNextClass(firstClassBean.classificationlist.get(0).classificationid);
        hideProgress();

//        FirstClassAdapter firstClassAdapter = new FirstClassAdapter(firstClassBean, getActivity());
        lv.setAdapter(firstClassAdapter);

        //一级列表点击
        firstClassAdapter.setClickFirstClassListener(new FirstClassAdapter.clickFirstClassListener() {
            @Override
            public void showNextClass(String id, int position) {
                if (img_urls.size() > 0) {
                    img_urls.clear();
                }
                img_urls.add(firstClassBean.classificationlist.get(position).imgurl);
                showBanner(img_urls);
                showProgress();
                presenter.getIndexNextClass(id);
            }
        });
        firstClassAdapter.notifyDataSetChanged();
    }

    private void showBanner(List<String> stringList) {
        if (stringList.size() > 0) {
            RequestOptions options = new RequestOptions().bitmapTransform(new RoundedCorners(30));
            Glide.with(getActivity()).load(stringList.get(0))
                    .apply(options)
                    .into(carouselView);
        }
    }

    @Override
    public void indexClassFailure(String msg) {
        hideProgress();
        ToastUtils.showLongToast(msg);
    }

    @Override
    public void indexNextClassSuccess(NextClassBean nextClassBean) {
        hideProgress();
        setAdapter(nextClassBean.classificationlist);
    }

    @Override
    public void indexNextClassFailure(String msg) {
        hideProgress();
        ToastUtils.showLongToast(msg);
    }

    private void setAdapter(List<NextClassBean.ClassificationlistBeanX> classificationlist) {
        NextClassAdapter nextClassAdapter = new NextClassAdapter(R.layout.class_child_item_layout, classificationlist);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(nextClassAdapter);
    }

}
